package service.impl;

import dao.RentMapper;
import entity.Rent;
import org.apache.ibatis.session.SqlSession;
import service.RentService;
import utils.MybatisUtil;

import java.util.List;

public class RentServiceImpl implements RentService {
    SqlSession sqlSession;
    @Override
    public List<Rent> list(int pageIndex,int pageSize) {
        int startIndex = (pageIndex - 1) * pageSize;
        List<Rent> list = null;
        try{
            sqlSession = MybatisUtil.createSqlSession();
            list = sqlSession.getMapper(RentMapper.class).list(startIndex,pageSize);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MybatisUtil.closeSqlSession(sqlSession);
        }
        return list;
    }

    @Override
    public int pageCount(int pageSize) {
        int pageCount = 0;
        try {
            sqlSession = MybatisUtil.createSqlSession();
            int count = sqlSession.getMapper(RentMapper.class).count();
            pageCount = count % pageSize == 0 ?(count / pageSize) : (count / pageSize) + 1;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtil.closeSqlSession(sqlSession);
        }
        return pageCount;
    }

    @Override
    public int update(int id,int status) {
        int r = 0;
        try {
            sqlSession = MybatisUtil.createSqlSession();
            r = sqlSession.getMapper(RentMapper.class).update(id,status);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        } finally {
            MybatisUtil.closeSqlSession(sqlSession);
        }
        return r;
    }

    @Override
    public int delete(int id) {
        int r = 0;
        try {
            sqlSession = MybatisUtil.createSqlSession();
            r = sqlSession.getMapper(RentMapper.class).delete(id);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        } finally {
            MybatisUtil.closeSqlSession(sqlSession);
        }
        return r;
    }

    @Override
    public int add(Rent rent) {
        int r = 0;
        try {
            sqlSession = MybatisUtil.createSqlSession();
            r = sqlSession.getMapper(RentMapper.class).add(rent);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        } finally {
            MybatisUtil.closeSqlSession(sqlSession);
        }
        return r;
    }

    @Override
    public List<Rent> listByUserName(int pageIndex, int pageSize, String userName) {
        int startIndex = (pageIndex - 1) * pageSize;
        List<Rent> list = null;
        try{
            sqlSession = MybatisUtil.createSqlSession();
            list = sqlSession.getMapper(RentMapper.class).listByUserName(startIndex,pageSize,userName);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            MybatisUtil.closeSqlSession(sqlSession);
        }
        return list;
    }


}
